import {makeAutoObservable} from "mobx";
import {List} from "antd";
import ArrayUtils from "@zxy-cn/array-utils";
import React from "react";

export interface DashboardPageInfo {
    key:React.Key
    path: string
    title?: string
}

export class DashboardPageStore {
    constructor() {
        makeAutoObservable(this)
    }

    pages: Array<DashboardPageInfo> = []

    push(path: string, title: string) {
        this.pages.push({path, title,key:Date.now().toString()})
    }

    clear(){
        this.pages = [];
    }

    remove(path: string) {
        let dashboardPageInfos = Array.from(this.pages);

        ArrayUtils.removeBy(dashboardPageInfos, value => {
            return value.path === path
        })
        this.pages = dashboardPageInfos
    }

    setTitle(path:string,title:string){
        let dashboardPageInfos = Array.from(this.pages);
        let find = dashboardPageInfos.find(it=>it.path===path);
        if (find){
            find.title = title
            this.pages = dashboardPageInfos
        }
    }

    get currentPageTitle(){
        if (!this.pages.length){
            return undefined
        }
        return  this.pages[this.pages.length-1].title
    }

    reload(path:string){
        let find = this.pages.find(it=>it.path===path);
        if (find){
            find.key = Date.now().toString();
        }
    }

}

export default new DashboardPageStore()